﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;

namespace University.Workshop.WinApp
{
    public partial class AssignRole : Form
    {
        string loginUser;
        string ciLoginUser;
        string ciUserSelected = "";
        public AssignRole()
        {
            InitializeComponent();
            labelDate.Text = DateTime.Now.Date.ToShortDateString();
            ciLoginUser = Main.userMenu[3].ToString();
            loginUser = Main.userMenu[0] + " " + Main.userMenu[1].ToString();
            labelLogin.Text = loginUser;
            loadUsers();
            IssuePermits();
        }

        private void IssuePermits()
        {
            //buttonAddRole.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        }

        private void loadUsers()
        {            
            comboBoxUser.Items.Clear();
            comboBoxCiUsers.Items.Clear();
            DataTable temporalData = new DataTable();
            temporalData = UserManager.LoadDataGridViewUser();
            for (int i = 0; i < temporalData.Rows.Count; i++)
            {
                comboBoxCiUsers.Items.Add(temporalData.Rows[i].ItemArray[0].ToString());
                comboBoxUser.Items.Add(temporalData.Rows[i].ItemArray[1].ToString()+" "+temporalData.Rows[i].ItemArray[2].ToString());
            }
        }

        private void loadRolesAvailableForTheUserSelected(string ciUser)
        {
            comboBoxRole.Items.Clear();
            DataTable temporalData = new DataTable();
            temporalData = FunctionsManager.LoadRolesAvailableForTheUserSelected(ciUser);
            for (int i = 0; i < temporalData.Rows.Count; i++)
            {
                comboBoxRole.Items.Add(temporalData.Rows[i].ItemArray[0].ToString());
            }
        }

        private void LoadDataRolesOfTheUser(string ciUser)
        {
            listBoxRoleOfUserSelected.Items.Clear();
            DataTable temporalData = new DataTable();            
            temporalData = FunctionsManager.LoadDataRolesOfTheUser(ciUser);
            for (int i = 0; i < temporalData.Rows.Count; i++)
            {
                listBoxRoleOfUserSelected.Items.Add(temporalData.Rows[i].ItemArray[0].ToString());
            }
        }

        private void comboBoxUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                comboBoxCiUsers.SelectedIndex = int.Parse(comboBoxUser.SelectedIndex.ToString());
                ciUserSelected = comboBoxCiUsers.SelectedItem.ToString();
                LoadDataRolesOfTheUser(ciUserSelected);
                loadRolesAvailableForTheUserSelected(ciUserSelected);
            }
            catch (Exception err)
            {

                MessageBox.Show("Error: "+err.Message.ToString());
            }
        }


        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AssignRole_FormClosed(object sender, FormClosedEventArgs e)
        {
            Singleton.Instance.valid = true;
        }

        private void buttonAddRole_Click(object sender, EventArgs e)
        {
            if (comboBoxUser.SelectedItem != null)
            {
                if (comboBoxRole.SelectedItem != null)
                {
                    if (FunctionsManager.AddRoleToUser(ciUserSelected, comboBoxRole.SelectedItem.ToString()))
                    {
                        MessageBox.Show("Added Role successfully");
                        LoadDataRolesOfTheUser(ciUserSelected);
                        loadRolesAvailableForTheUserSelected(ciUserSelected);
                        comboBoxRole.SelectedItem = null;
                        comboBoxRole.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("Role not added to user");
                    }
                }
                else
                {
                    MessageBox.Show("You must first select a Role");
                }
            }
            else
            {
                MessageBox.Show("You must first select a User");
            }
        }

        private void buttonRemoveRole_Click(object sender, EventArgs e)
        {
            if (comboBoxUser.SelectedItem != null)
            {
                if (listBoxRoleOfUserSelected.SelectedItem != null)
                {
                    DialogResult confirm = MessageBox.Show("Are you sure to delete the  function?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                    if (confirm.Equals(DialogResult.OK))
                    {
                        if (FunctionsManager.RemoveRoleOfTheUser(ciUserSelected, listBoxRoleOfUserSelected.SelectedItem.ToString()))
                        {
                            MessageBox.Show("Delete Role successfully");
                            loadRolesAvailableForTheUserSelected(ciUserSelected);
                            LoadDataRolesOfTheUser(ciUserSelected);
                            comboBoxRole.SelectedItem = null;
                            comboBoxRole.Text = "";
                            listBoxRoleOfUserSelected.SelectedItem = null;
                        }
                        else
                        {
                            MessageBox.Show("Role not eliminated");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("You must first select a role to delete");
                }
            }
            else
            {
                MessageBox.Show("You must first select a User");
            }
        }
    }
}
